/*
 * 		Author : Xerxes
 * 		Program: 
 * 		Created On : 01.10.2011 15:54:23 BDT
 *      "I have not failed, I have just found 10000 ways that won't work.
 * 
*/


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,int> >
#define mmk( x, y , z ) mk( x, mk( y, z ) )

inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
int vis[205];
int N;
int E;
int b,w;

vector< VI > go;
inline void Read(){
	CI(N);
	CI(E);
	int x,y;
	go.assign(N,VI() );
	fo(i,0,E){
		CI(x);
		CI(y);
		go[x].pb(y);
		go[y].pb(x);
	}
	
}
bool possible;
inline void colorit(int father, int color){
	if( possible==false ) return ;
	if( vis[ father ] == -1 ){
		color == 0 ? ++b : ++w;
		vis[father] = color;
		VI child = go[ father ];
		
		for(int i=0, j = SZ(child); i<j; ++i ){
			colorit( child[i], color == 0 ? 1 : 0 );
		}
	}
	else{
		if( vis[ father ]!= color ){
			possible=false;
		}
	}
}
inline void Proc(){
	SET(vis,-1);
	possible=true;
	
	int retval = 0;
	
	fo(i,0,N){
		if( vis[i]==-1 ){
			b=w=0;
			colorit(i,0);
			int var = min(b,w);
			if( var==0 ){
				retval+=max(b,w);
			}
			else{
				retval+=var;
			}
		}
	}
	if( possible==false ){
		cout<<-1<<"\n";
	}
	else{
		cout<<retval<<"\n";
	}
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","rt",stdin);
    #endif
    int cases=1;
    for( CI(cases) ; cases ; --cases ){
			Read();
			Proc();
    }

return 0;
}


